package com.exam.serviceimpl;
import com.exam.entity.PracticeQuestion;
import com.exam.entity.WrongQuestion;
import com.exam.mapper.PracticeQuestionRepository;
import com.exam.mapper.WrongQuestionRepository;
import com.exam.service.PracticeQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
public class PracticeQuestionServiceImpl implements PracticeQuestionService {

    @Autowired
    private WrongQuestionRepository wrongQuestionRepository;

    @Autowired
    private PracticeQuestionRepository practiceQuestionRepository;

    @Override
    public Page<PracticeQuestion> generatePracticeQuestionsFromWrongQuestions(Pageable pageable) {
        // 查询分页的错题
        Page<WrongQuestion> wrongQuestions = wrongQuestionRepository.findAll(pageable);

        // 将错题转换为练习题
        for (WrongQuestion wrongQuestion : wrongQuestions) {
            PracticeQuestion practiceQuestion = new PracticeQuestion();
            practiceQuestion.setQuestionId(wrongQuestion.getQuestionId());
            practiceQuestion.setQuestionType(wrongQuestion.getQuestionType());
            practiceQuestion.setCourse(wrongQuestion.getCourse());
            practiceQuestion.setQuestionContent(wrongQuestion.getCorrectAnswer()); // 设置题目内容
            practiceQuestion.setCorrectAnswer(wrongQuestion.getCorrectAnswer());
            practiceQuestion.setExplanation(wrongQuestion.getExplanation());
            practiceQuestionRepository.save(practiceQuestion);
        }

        // 返回练习题的分页结果
        return practiceQuestionRepository.findAll(pageable);
    }
}